﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;
using Utility;
using System.Configuration;

public partial class Admin_NguoiMua : System.Web.UI.Page
{
    protected string CurrentColumnSort
    {
        get
        {
            return ViewState["CurrentColumnSort"].ToString();
        }
        set
        {
            ViewState["CurrentColumnSort"] = value;
        }
    }
    protected int STT
    {
        get
        {
            return int.Parse(ViewState["STT"].ToString());
        }
        set
        {
            ViewState["STT"] = value;
        }
    }
    protected string OrderType
    {
        get
        {
            return ViewState["OrderType"].ToString();
        }
        set
        {
            ViewState["OrderType"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.CurrentColumnSort = NguoiMuaColumn.TenDangNhap_NguoiMua.ToString();
            this.OrderType = OrderDirection.ASC.ToString();
            LoadData();
        }
    }
    private void LoadData()
    {
        try
        {
            if (int.Parse(Session["MaPhanQuyen"].ToString()) == 0)
            {
                btn_CapNhatTrangThai.Visible = true;

            }
            else
            {
                btn_CapNhatTrangThai.Visible = false;
            }
            STT = (pager.CurrentIndex - 1) * pager.PageSize + 1;
            int total = 0;
            pager.PageSize = 5;// int.Parse(ConfigurationManager.AppSettings["pagesize"]);

            Criteria cri = new Criteria();
            if (txt_tennsx.Text != "")
                cri.add(Condition.AND, NguoiMuaColumn.TenNguoiMua.ToString(), Condition.LIKE, txt_tennsx.Text);
            NguoiMuaCollection NMCollection = NguoiMuaBUS.GET(cri.Criter, pager.CurrentIndex, pager.PageSize, NguoiMuaColumn.TenNguoiMua.ToString(), OrderDirection.ASC.ToString(), out total);
            pager.ItemCount = total;
            rpt_NguoiMua.DataSource = NMCollection;
            rpt_NguoiMua.DataBind();
            if (pager.ItemCount <= 0 || pager.ItemCount <= pager.PageSize)
                pager.Visible = false;
            else
                pager.Visible = true;
        }
        catch
        {

        }
    }
    protected void btn_timkiem_Click(object sender, EventArgs e)
    {
        LoadData();
    }
    protected void rpt_NguoiMua_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem
            || e.Item.ItemType == ListItemType.Item)
        {
            NguoiMua NM = (NguoiMua)e.Item.DataItem;

            Label lb_STT = (Label)e.Item.FindControl("lb_STT");
            lb_STT.Text = Convert.ToString(STT + e.Item.ItemIndex);


            HiddenField hf_TenDangNhap = (HiddenField)e.Item.FindControl("hf_TenDangNhap");
            hf_TenDangNhap.Value = NM.TenDangNhap_NguoiMua.ToString();

            CheckBox cb_TrangThai = (CheckBox)e.Item.FindControl("cb_TrangThai");
            if (NM.TrangThai == 1)
            {
                cb_TrangThai.Checked = true;
            }
            else
            {
                cb_TrangThai.Checked = false;
            }


        }
    }
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();
    }
    protected void btn_CapNhatTrangThai_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < rpt_NguoiMua.Items.Count; i++)
        {
            RepeaterItem item = rpt_NguoiMua.Items[i];
            CheckBox cb_TrangThai = (CheckBox)item.FindControl("cb_TrangThai");
            if (cb_TrangThai.Checked == true)
            {
                HiddenField hf_TenDangNhap = (HiddenField)item.FindControl("hf_TenDangNhap");
                NguoiMuaBUS.UpdateTrangThai(hf_TenDangNhap.Value, 1);
            }
            else
            {
                HiddenField hf_TenDangNhap = (HiddenField)item.FindControl("hf_TenDangNhap");
                NguoiMuaBUS.UpdateTrangThai(hf_TenDangNhap.Value, 0);
            }
        }
        LoadData();
    }
}